package com.demo.qqClient;

import com.demo.qqClient.service.FileClientService;
import com.demo.qqClient.service.MessageClientService;
import com.demo.qqClient.service.UserClientService;
import com.demo.qqClient.utils.Utility;

import javax.sound.midi.Soundbank;

/**
 * 用于显示客户端界面
 *
 * @author gy
 * @date 2022/5/5
 */
public class QQView {
    /**
     * 控制是否显示菜单
     */
    private boolean loop = true;
    /**
     * 接收用户的键盘输入
     */
    private String key = "";

    /**
     * 用于登录服务器，注册用户
     */
    private UserClientService userClientService = new UserClientService();

    /**
     * 用于 客户端 私聊
     */
    private MessageClientService messageClientService = new MessageClientService();

    /**
     * 该对象用于 传输文件
     */
    private FileClientService fileClientService = new FileClientService();

    public static void main(String[] args) {
        new QQView().viewMenu();
        System.out.println("客户端退出系统。。。。。");
    }

    /**
     * 显示主菜单
     */
    public void viewMenu() {
        while (loop) {
            System.out.println("=============欢迎登陆网络通信系统============");
            System.out.println("\t\t 1 登录系统");
            System.out.println("\t\t 9 退出系统");
            System.out.print("请输入你的选择:");
            key = Utility.readString(1);

            // 根据用户的输入，来处理不同的逻辑
            switch (key) {
                case "1":
                    System.out.print("请输入用户号:");
                    String userId = Utility.readString(50);
                    System.out.print("请输入密 码 :");
                    String pwd = Utility.readString(50);
                    // 接下来处理就比较复杂了
                    // 需要到服务端去验证是否合法
                    if (userClientService.checkUser(userId,pwd)){
                        System.out.println("=============欢迎(用户"+userId+"登录成功) ============");
                        // 进入二级菜单
                        while (loop){
                            System.out.println("====网络通信系统二级菜单(用户"+userId+")============");
                            System.out.println("\t\t 1 显示在线用户列表");
                            System.out.println("\t\t 2 群发消息");
                            System.out.println("\t\t 3 私发消息");
                            System.out.println("\t\t 4 发送文件");
                            System.out.println("\t\t 9 退出系统");
                            System.out.print("请输入你的选择:");

                            key = Utility.readString(1);
                            switch (key) {
                                case "1":
                                    System.out.println("显示在线用户列表");
                                    userClientService.onlineFriendList();
                                    break;
                                case "2":
                                    System.out.println("请输入想对大家说的话: ");
                                    String s = Utility.readString(100);
                                    // 调用一个方法，将消息发给服务器
                                    messageClientService.sendMessageToAll(s,userId);
                                    break;
                                case "3":
                                    System.out.println("请输入想聊天的用户号(在线):");
                                    String getterId = Utility.readString(50);
                                    System.out.println("请输入想说的话:");
                                    String content = Utility.readString(100);
                                    // 编写一个方法，将消息发送给服务器端
                                    messageClientService.sendMessageToOne(content,userId,getterId);
                                    break;
                                case "4":
                                    System.out.print("请输入你想把文件发送给的用户id(在线用户) :");
                                    getterId = Utility.readString(50);
                                    System.out.print("请输入发送文件的路径 (eg: d:\\xxx.png) :");
                                    String src = Utility.readString(100);
                                    System.out.print("请输入发送到 对方 的路径 (eg: e:\\xxx.png) :");
                                    String target = Utility.readString(100);
                                    fileClientService.sendFileToOne(src,target,userId,getterId);
                                    break;
                                case "9":
                                    // 调用方法，给服务器发送一个退出系统的 msg
                                    userClientService.logOut();
                                    loop = false;
                                    break;
                                default:

                            }
                        }

                    }else {
                        System.out.println("========登录服务器失败========");
                    }

                    break;
                case "9":
                    loop = false;
                    break;
                default:
            }

        }
    }
}
